/**
 * ...
 * @author Thot
 */

function SaveProjectCommand(){}; 
POO.extend( SaveProjectCommand, Command );

// p_data.name

SaveProjectCommand.prototype.execute = function( p_notificationType, p_data )
{
	
	var facade = ApplicationFacade.getInstance(); 
	
	var appProxy = facade.getProxy( ApplicationProxy.NAME );
	var mapProxy = facade.getProxy( MapProxy.NAME );
	var animProxy = facade.getProxy( AnimationProxy.NAME );
	var texProxy = facade.getProxy( TextureProxy.NAME );
	
	
	var currentProject = appProxy.getProjectName();
	var maps = mapProxy.getData();
	
	
	var data = 	"(function(){";
		data += "\n\tvar project = new Object();";
		data += "\n\tproject.name = \""+p_data.name+"\";";
		
		data += "\n\n\tproject.getAnimationsList = function(){";
		data += this.exportAnimations(animProxy);
		data += "\n\t};";
		
		data += "\n\n\tproject.getTexturesList = function(){";
		data += this.exportTextures(texProxy);
		data += "\n\t};";
		
		data += "\n\n\tproject.getMapsList = function(){";
		data += this.exportMaps(maps);
		data += "\n\t};";
		
		data += "\n\n\treturn project;})();";
	
	var url = "./assets/projects/"+p_data.name+".js";
	var sender = new Sender();
	sender.sendAndLoad("./service/saver.php", "url="+url+"&save="+data, "POST", true);
};

SaveProjectCommand.prototype.exportTextures = function( p_proxy )
{
	return "return [\n\t\t'"+p_proxy.getTexturesList().join("',\n\t\t'")+"'];";
};

SaveProjectCommand.prototype.exportAnimations = function( p_proxy )
{
	
	var anims = p_proxy.getData();
	var frameObj = null;
	var vo = null;
	var i = 0;
	var j = 0;
	var max = anims.length;
	var frames = anims.length;
	var data = "\n\n\treturn [";
	
	for( i = 0; i < max; i++ )
	{
		vo = anims[i];
		
		if( i > 0 )
			data += ",";
		
		data += "\n\t\t{";
		
		data += '\n\t\t\t"name":"'+vo.name+'",';
		data += '\n\t\t\t"width":'+vo.width+',';
		data += '\n\t\t\t"height":'+vo.height+',';
		data += '\n\t\t\t"animTime":'+vo.animTime+',';
		data += '\n\t\t\t"frames":[';
		
		max2 = vo.frames.length;
		
		for( j = 0; j < max2; j++ )
		{
			frameObj = vo.frames[j];
			
			if( j > 0 )
				data += ",";
				
			data+= '\n\t\t\t\t{"uv":['+frameObj.uv.join()+'],"tex":'+frameObj.tex+'}';
		}
		
		data += '\n\n\t\t]}'
	}
	
	data += "\n\n\t];";
	
	return data;
};

SaveProjectCommand.prototype.exportMaps = function( p_maps )
{
	var i = 0;
	var max = p_maps.length;
	var layerVO = null;
	var data = "\n\n\treturn [";
	
	for( i = 0; i < max; i++ )
	{
		mapVO = p_maps[i];
		
		if( i > 0 )
		{
			data += ",";
		}
		data +=  '\n\t\t{"name":"'+mapVO.name+'","type":"isomap","layers":['+this.exportLayers(mapVO.layers)+']}';
	}
	
	data += "\n\n\t];";
	
	return data;
};

SaveProjectCommand.prototype.exportLayers = function( p_layers )
{
	var i = 0;
	var max = p_layers.length;
	var layerVO = null;
	var data = "";
	
	for( i = 0; i < max; i++ )
	{
		layerVO = p_layers[i];
		
		if( i > 0 )
		{
			data += ",";
		}
		data +=  '\n\t\t\t{"lazy":'+layerVO.lazy+',"sortFunc":'+layerVO.sortFunc+',"sortArgs":'+layerVO.sortArgs+',"name":"'+layerVO.name+'","displayList":['+this.exportDisplayList(layerVO.displayList)+'\n\t\t]}';
	}
	
	return data;
							
};

SaveProjectCommand.prototype.exportDisplayList = function( p_displayList )
{
	
	//x,y,z,width,height,alpha,animated:true||false,texId||animId,movable:true||false
	
	var i = 0;
	var j = 0;
	var max = p_displayList.length;
	var max2 = p_displayList.length;
	var spr = null;
	var data = "";
	
	for( i = 0; i < max; i++ )
	{
		spr = p_displayList[i];
		
		if( i > 0 )
		{
			data += ",";
		}
		
		max2 = spr.length;
		data +=  '\n\t\t\t[';
		
		for( j = 0; j < max2; j++ )
		{
			if( j > 0 )
			{
				data += ",";
			}
			
			if( typeof( spr[j] ) == "string" )
			{
				data += '"'+spr[j]+'"';
			}
			else
			{
				data += spr[j];
			}
		}
		
		data += ']';
		
	}
	
	return data;
};